package com.atguigu.gmall.common.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-20 16:48
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RabbitService {

	private final RabbitTemplate rabbitTemplate;

	/**
	 * 表示成功发送消息
	 */
	public void sendMessage(String exchange, String routingKey, Object message) {
		rabbitTemplate.convertAndSend(exchange, routingKey, message);
	}

	/**
	 * 发送延时消息
	 */
	public boolean sendDelayMessage(String exchange, String routingKey, Object message, Integer delayTime) {
		rabbitTemplate.convertAndSend(exchange, routingKey, message, msg -> {
			log.info("交换机:{}\n路由键:{}\n消息:{}\n延迟时间:{}\n", exchange, routingKey, message, delayTime);
			msg.getMessageProperties().setDelay(delayTime * 1000);
			return msg;
		});
		return true;
	}

}
